package com.mango.HDFSTools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import com.mango.Tools.FileTools;

public class HDFSAPI {
	private static InputStream in;
	private static OutputStream out;
	private static FileSystem fileSystem;
	private static String URIADDRESS = "hdfs://ns1/";
	Configuration conf = null;

	public HDFSAPI() throws IOException, InterruptedException, URISyntaxException {
		// 创建一个配置实例
		this.conf = new Configuration();
		// conf.set("fs.defaultFS", "hdfs://hadoop.edu360.cn:9000");
		// 链接hdfs文件系统
		fileSystem = FileSystem.get(new URI(URIADDRESS), conf, "root");
	}

	public HDFSAPI(Configuration conf) throws IOException, InterruptedException, URISyntaxException {
		// 创建一个配置实例
		this.conf = conf;
		// conf.set("fs.defaultFS", "hdfs://hadoop.edu360.cn:9000");
		// 链接hdfs文件系统
		fileSystem = FileSystem.get(new URI(URIADDRESS), conf, "root");
	}

	public void testUpload() {

		try {
			in = new FileInputStream("d:\\out123.txt");
			out = fileSystem.create(new Path("/out123.txt"));
			IOUtils.copyBytes(in, out, 1024, true);

		} catch (FileNotFoundException e) {
			System.out.println("file exception" + e.getLocalizedMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("有异常" + e.getMessage());
			e.printStackTrace();
		}

	}

	public void testDownload() {
		try {
			// 上传文件的时候要考虑用户权限问题，否则会出错
			System.setProperty("HADOOP_USER_NAME", "root");// 欺骗程序说是root用户在执行此操作，伪装成root用户
			// 打开一个输入流
			in = fileSystem.open(new Path("/wc.txt"));
			// 打开本地输出流文件
			out = new FileOutputStream("d://out123.txt");
			// 拷贝IN-》OUT
			IOUtils.copyBytes(in, out, 1024, true);
			// 关闭流 因为ioutils已经关了 所以不用管
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// @Test
	public void testMkdir() throws IllegalArgumentException, IOException {
		fileSystem.mkdirs(new Path("/a/b"));
	}

	// @Test
	public void testDelDir() throws IllegalArgumentException, IOException {
		fileSystem.delete(new Path("/a"), true);// 第二个参数代表是否递归删除
	}

	// @After
	public void orpOver() throws IOException {
		fileSystem.close();
	}

	/**
	 * 删除指定文件或文件夹
	 * 
	 * @param path
	 * @throws IOException
	 */
	public void delDirectory(Path path) throws IOException {
		if (fileSystem.exists(path))
			fileSystem.delete(path, true);
	}

	@Test
	public void createDirectory(Path path) {
		try {
			if (!fileSystem.exists(path)) {
				fileSystem.mkdirs(path);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("创建input 目录失败");
		}
	}

	public void closeFilseSystem() {
		try {
			fileSystem.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public <T> void CheckPath(T[] inputPaths, T[] outputPaths) throws IOException {
		// 判断inputpath
		if (inputPaths.getClass().getComponentType() == File.class) {
			File[] paths = (File[]) inputPaths;
			for (File file : paths) {
				if (file.isFile())
					continue;
				if (file.isDirectory()) {
					if (!file.exists())
						file.mkdirs();
				}
			}
		} else {
			Path[] paths = (Path[]) inputPaths;
			for (Path path : paths) {
				createDirectory(path);
			}
		}
		// 判断outputpath
		if (outputPaths.getClass().getComponentType() == Path.class) {
			Path[] paths = (Path[]) outputPaths;
			for (Path path : paths) {
				delDirectory(path);
			}
		} else {
			File[] paths = (File[]) outputPaths;
			for (File file : paths) {
				FileTools.delFileOrDir(file);
			}
		}
	}
}
